package org.cosmact.designer.vxml.gefeditor.editpart.palette;

import org.cosmact.designer.vxml.gefeditor.editor.ModelCreationFactory;
import org.cosmact.designer.vxml.gefeditor.i118n.VxmlEditorMessages;
import org.cosmact.designer.vxml.gefeditor.uiresources.EIcons;
import org.cosmact.designer.vxml.gefeditor.uiresources.UIResource;
import org.cosmact.vxml.model.Dialog;
import org.eclipse.draw2d.Graphics;
import org.eclipse.gef.palette.CombinedTemplateCreationEntry;
import org.eclipse.gef.palette.ConnectionCreationToolEntry;
import org.eclipse.gef.palette.MarqueeToolEntry;
import org.eclipse.gef.palette.PaletteContainer;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.PaletteToolbar;
import org.eclipse.gef.palette.PanningSelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gef.requests.CreationFactory;

/**
 * Utility class that can create a GEF Palette.
 * 
 * @see #createPalette()
 * @author Elias Volanakis
 */
public final class DocumentEditorPaletteFactory {

	/** Preference ID used to persist the palette location. */
	private static final String PALETTE_DOCK_LOCATION = "ShapesEditorPaletteFactory.Location";
	/** Preference ID used to persist the palette size. */
	private static final String PALETTE_SIZE = "ShapesEditorPaletteFactory.Size";
	/** Preference ID used to persist the flyout palette's state. */
	private static final String PALETTE_STATE = "ShapesEditorPaletteFactory.State";

	/** Create the "Shapes" drawer. */
	private static PaletteContainer createShapesDrawer() {
		PaletteDrawer componentsDrawer = new PaletteDrawer(
				VxmlEditorMessages.EditorPaletteDialogCategoryTitle);

		//$NON-NLS-N$
		CombinedTemplateCreationEntry component = new CombinedTemplateCreationEntry(
				VxmlEditorMessages.EditorPaletteDialogTitle,
				VxmlEditorMessages.EditorPaletteDialogTitle, Dialog.class,
				new ModelCreationFactory(Dialog.class), UIResource
						.getImageDescriptor(EIcons.ELLIPSE16), UIResource
						.getImageDescriptor(EIcons.ELLIPSE24));
		componentsDrawer.add(component);

		return componentsDrawer;
	}

	/**
	 * Creates the PaletteRoot and adds all palette elements. Use this factory
	 * method to create a new palette for your graphical editor.
	 * 
	 * @return a new PaletteRoot
	 */
	public static PaletteRoot createPalette() {
		PaletteRoot palette = new PaletteRoot();
		palette.add(createToolsGroup(palette));
		palette.add(createShapesDrawer());
		return palette;
	}

	/** Create the "Tools" group. */
	private static PaletteContainer createToolsGroup(PaletteRoot palette) {
		PaletteToolbar toolbar = new PaletteToolbar("Tools");

		// Add a selection tool to the group
		ToolEntry tool = new PanningSelectionToolEntry();
		toolbar.add(tool);
		palette.setDefaultEntry(tool);

		// Add a marquee tool to the group
		toolbar.add(new MarqueeToolEntry());

		// Add (solid-line) connection tool
		tool = new ConnectionCreationToolEntry(
				VxmlEditorMessages.EditorPaletteConnectionToolTitle,
				VxmlEditorMessages.EditorPaletteConnectionToolTip,
				new CreationFactory() {
					public Object getNewObject() {
						return null;
					}

					public Object getObjectType() {
						return new Integer(Graphics.LINE_SOLID);
					}
				}, UIResource.getImageDescriptor(EIcons.CONNECTION_S16),
				UIResource.getImageDescriptor(EIcons.CONNECTION_S24));
		toolbar.add(tool);

		return toolbar;
	}

	/** Utility class. */
	private DocumentEditorPaletteFactory() {
		// Utility class
	}

}